<?php
/**
 * DMFramework
 * 
 * @license Copyright 2012 Dillen Meijboom under the Apache license version 2.0 (http://apache.org/licenses/LICENSE-2.0)
 * @author Dillen Meijboom <info@dillenm.nl>
 * @url http://dmframework.nl
 */

/**
 * This is not really a "class" but I need a file to output the error to the browser and
 * it looks more cooler this way.. (And it saves 1 view file).
 * 
 * @license Copyright 2012 Dillen Meijboom under the Apache license version 2.0 (http://apache.org/licenses/LICENSE-2.0)
 * @author Dillen Meijboom <info@dillenm.nl>
 * @package System.Core.Error
 */
class Dmf_Error_View
{
    /**
     * Error handler instance
     * 
     * @var Dmf_Error_Handler
     */
    public $error;
    
    /**
     * Constructor
     * 
     * @param Dmf_Error_Handler $error 
     */
    public function __construct( Dmf_Error_Handler $error )
    {
        $this->error = $error;
    }
    
    /**
     * Render this view 
     */
    public function render()
    {
?>
<html>
    <head>
        <title>DMFramework error &bull; <?php echo $this->error->exceptionName; ?></title>
        <style type="text/css">
            * {
                margin: 0px;
                padding: 0px;
                font-family: Calibri, Arial;
                font-size: 12px;
            }
            
            html, body {
                width: 100%;
                height: 100%;
                display: table;
            }
            
            .wrap {
                display: table-cell; 
                vertical-align: middle;
            }
            
            .container {
                margin: auto;
                width: 750px;
                display: block;
                border: 1px solid #DDD;
                background-color: #F9F9F9;
            }
            
            .title {
                width: 730px;
                padding: 10px;
                font-size: 14px;
                font-weight: bold;
                background-color: #EEE;
                border-bottom: 1px solid #DDD;
            }
            
            .error {
                padding: 10px;
            }
            
            .info label {
                display: block;
                float: left;
                width: 120px;
                font-weight: bold;
            }
            
            .source {
                border-radius: 10px;
                -moz-border-radius: 10px;
                -webkit-border-radius: 10px;
                -o-border-radius: 10px;
                background-color: #FFF;
                padding: 15px;
                color: #444;
                overflow: hidden;
                line-height: 16px;
                border: 1px solid #EEE;
            }
            
            .source pre {
                font-family: Lucida Console;
                font-size: 12px;
            }
        </style>
    </head>
    <body>
        
        <div class="wrap">
            <div class="container">
                <p class="title">There has been an error</p>
                <div class="error">
                    <?php echo $this->error->message; 
                        if ( Dmf_Application::getInstance()->getConfig()->asObject()->application->debug )
                        {
                    ?><br /><br />
                    
                    <div class="info">
                        <label>Name:</label> <?php echo $this->error->exceptionName; ?><br />
                        <label>File:</label> <?php echo $this->error->file; ?><br />
                        <label>Line:</label> <?php echo $this->error->line; ?><br />
                        <label>Code:</label> <?php echo $this->error->code; ?><br />
                    </div>
                    <b>Source:</b><br />
                    <br />
                    <div class="source">
                        <pre><?php
                            $lineNumber = 0;
                            
                            if ( file_exists( $this->error->file ) )
                            {
                                foreach( explode( "\n", file_get_contents( $this->error->file ) ) as $line )
                                {
                                    $lineNumber++;

                                    if ( ( $lineNumber - 4 ) < $this->error->line && ( $lineNumber + 4 ) > $this->error->line )
                                    {
                                        if ( $lineNumber == $this->error->line )
                                        {
                                            echo '<div style="background-color:#B65050;color:#000;"><pre>' . $lineNumber . ')' . "\t" . htmlentities( $line ) . '</pre></div>';
                                        }
                                        else
                                        {
                                            echo $lineNumber . ')' . "\t" . htmlentities( $line );
                                        }
                                    }
                                }
                            }
                            else
                            {
                                 echo '<div style="background-color:#B65050;color:#000;"><pre>' . $lineNumber . ')' . "\t" . 'Error file could not be loaded to display content!</pre></div>';
                            }
                        ?></pre>
                    </div>

                    <br /><br />

                    <b>Stack Trace:</b><br />

                    <?php
                        $nr = 0;

                        foreach( $this->error->stackTrace as $trace )
                        {
                            $nr++;

                            if ( isset( $trace['file'] ) && isset( $trace['line'] ) && isset( $trace['class'] ) && isset( $trace['function'] ) )
                            {
                                echo '&nbsp;&nbsp;&nbsp;&nbsp;' . $nr . ') ' . $trace['file'] . ' on line <i>' . $trace['line'] . '</i><br />';
                                echo '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' . $trace['class'] . ' ' . $trace['type'] . ' ' . $trace['function'] . '<br />';
                            }
                            else
                            {
                                if ( isset( $trace['file'] ) && isset( $trace['line'] ) )
                                {
                                    echo '&nbsp;&nbsp;&nbsp;&nbsp;' . $nr . ') ' . $trace['file'] . ' on line <i>' . $trace['line'] . '</i><br />';
                                }

                                if ( isset( $trace['class'] ) && isset( $trace['function'] ) )
                                {
                                    echo '&nbsp;&nbsp;&nbsp;&nbsp;' . $nr . ') ' . $trace['class'] . ' ' . $trace['type'] . ' ' . $trace['function'] . '<br />'; 
                                }
                            }

                            echo '<br />';
                        }
                        }
                    ?>
                </div>
            </div>
        </div>
        
    </body>
</html>
<?php
    }
}